package com.zenni.utils;

import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by douglasj on 4/14/16.
 */
public class TestNGExcutor {
    public  static String browser = System.getProperty("BROWSER_TYPE");
    private void runningTest(Map<String,String> parameters){
        //Create an instance on TestNG
        TestNG testNG = new TestNG();

        //Create an instance of XML Suite and assign a name for it.
        XmlSuite suite = getXmlSuite();

        //Create an instance of XmlTest and assign a name for it.
        XmlTest test = getXmlTest(suite);

        //Add any parameters that you want to set to the Test.
        test.setParameters(parameters);

        //Create a list which can contain the classes that you want to run.
        List<XmlClass> classes = getXmlClasses();

        //Assign that to the XmlTest Object created earlier.
        test.setXmlClasses(classes);

        //Create a list of XmlTests and add the Xmltest you created earlier to it.
        List<XmlTest> tests = new ArrayList<XmlTest>();
        tests.add(test);

        //add the list of tests to your Suite.
        suite.setTests(tests);

        //Add the suite to the list of suites.
        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        suites.add(suite);

        //Set the list of Suites to the testNG object you created earlier.
        testNG.setXmlSuites(suites);

        //invoke run() - this will run your class.
        testNG.run();
    }

    private XmlSuite getXmlSuite() {
        XmlSuite suite = new XmlSuite();
        suite.setName("Test Suite");
        return suite;
    }

    private XmlTest getXmlTest(XmlSuite suite) {
        XmlTest test = new XmlTest(suite);
        if(browser.equalsIgnoreCase("FF")){
            test.setName("test_with_firefox");
        }else if (browser.equalsIgnoreCase("CH")){
            test.setName("test_with_Chrome");
        }else if (browser.equalsIgnoreCase("AD")){
            test.setName("test_with_Android_Browser");
        }else if (browser.equalsIgnoreCase("IP")){
            test.setName("test_with_IOS_Safari");
        }
        return test;
    }

    private List<XmlClass> getXmlClasses() {
        List<XmlClass> classez = new ArrayList<XmlClass>();
        if (browser.equalsIgnoreCase("FF")){
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestCaseZenni_329"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestCaseZenni_893"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestCaseZenni_901"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestCaseZenni_1062"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestCaseZenni_898"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestFullSearchToOrderFlow"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestBestSellersFromHomePage"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestNewArrivalFromHomePageFlow"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestCaseZenni_1038"));
            classez.add(new XmlClass("com.zenni.test.desktopfirefox.TestCaseZenni_1039"));
        }else if (browser.equalsIgnoreCase("CH")){
            classez.add(new XmlClass("com.zenni.test.TestCaseZenni_329"));
            classez.add(new XmlClass("com.zenni.test.TestCaseZenni_893"));
            classez.add(new XmlClass("com.zenni.test.TestCaseZenni_901"));
            classez.add(new XmlClass("com.zenni.test.TestCaseZenni_1062"));
            classez.add(new XmlClass("com.zenni.test.TestCaseZenni_898"));
            classez.add(new XmlClass("com.zenni.test.TestFullSearchToOrderFlow"));
            classez.add(new XmlClass("com.zenni.test.TestBestSellersFromHomePage"));
            classez.add(new XmlClass("com.zenni.test.TestNewArrivalFromHomePageFlow"));
            classez.add(new XmlClass("com.zenni.test.TestCaseZenni_1038"));
            classez.add(new XmlClass("com.zenni.test.estCaseZenni_1039"));
        }else if (browser.equalsIgnoreCase("AD")){
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestCaseZenni_329"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestCaseZenni_893"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestCaseZenni_901"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestCaseZenni_1062"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestCaseZenni_898"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestFullSearchToOrderFlow"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestBestSellersFromHomePage"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestNewArrivalFromHomePageFlow"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestCaseZenni_1038"));
            classez.add(new XmlClass("com.zenni.test.mobileandroidtest.TestCaseZenni_1039"));
        }else if (browser.equalsIgnoreCase("IP")){
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestCaseZenni_329"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestCaseZenni_893"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestCaseZenni_901"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestCaseZenni_1062"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestCaseZenni_898"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestFullSearchToOrderFlow"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestBestSellersFromHomePage"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestNewArrivalFromHomePageFlow"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestCaseZenni_1038"));
            classez.add(new XmlClass("com.zenni.test.mobileiostest.TestCaseZenni_1039"));
        }
        return classez;
    }

    public static void main(String args[]) {
        TestNGExcutor testNGExcutor = new TestNGExcutor();
        Map<String,String> params = new HashMap<String,String>();
        params.put("BROWSER", browser);
        /*params.put("remoteUrl", "");
        params.put("domain", "http://www.google.com");*/
        testNGExcutor.runningTest(params);
    }
}
